package org.jeecg.modules.lab.maintain.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.device.entity.LabDevice;
import org.jeecg.modules.lab.device.service.ILabDeviceService;
import org.jeecg.modules.lab.maintain.entity.LabMaintain;
import org.jeecg.modules.lab.maintain.service.ILabMaintainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 仪器设备保养(维修)表
 * @Author: jeecg-boot
 * @Date: 2023-07-18
 * @Version: V1.0
 */
@Api(tags = "仪器设备保养(维修)表")
@RestController
@RequestMapping("/maintain/labMaintain")
@Slf4j
public class LabMaintainController extends JeecgController<LabMaintain, ILabMaintainService> {
    @Autowired
    private ILabMaintainService labMaintainService;

    @Autowired
    private ILabDeviceService labDeviceService;

    @Autowired
    private ILabCronJobService labCronJobService;

    /**
     * 分页列表查询
     *
     * @param labMaintain
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "仪器设备保养(维修)表-分页列表查询")
    @ApiOperation(value = "仪器设备保养(维修)表-分页列表查询", notes = "仪器设备保养(维修)表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<LabMaintain>> queryPageList(LabMaintain labMaintain,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<LabMaintain> queryWrapper = QueryGenerator.initQueryWrapper(labMaintain, req.getParameterMap());
        Page<LabMaintain> page = new Page<LabMaintain>(pageNo, pageSize);
        IPage<LabMaintain> pageList = labMaintainService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param labMaintain
     * @return
     */
    @AutoLog(value = "仪器设备保养(维修)表-添加")
    @ApiOperation(value = "仪器设备保养(维修)表-添加", notes = "仪器设备保养(维修)表-添加")
//	@RequiresPermissions("maintain:lab_maintain:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody LabMaintain labMaintain) {
        labMaintainService.save(labMaintain);


        if (MyEnum.sync) {
            long time = new Date().getTime();
            LabCronJob job = new LabCronJob("lab_maintain", "0", time, 1);
            labCronJobService.save(job);
        }
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param labMaintain
     * @return
     */
    @AutoLog(value = "仪器设备保养(维修)表-编辑")
    @ApiOperation(value = "仪器设备保养(维修)表-编辑", notes = "仪器设备保养(维修)表-编辑")
//	@RequiresPermissions("maintain:lab_maintain:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody LabMaintain labMaintain) {
        labMaintain.setSynFlag("1");
        labMaintainService.updateById(labMaintain);
        LabDevice device = labDeviceService.getById(labMaintain.getDevId());
        device.setSynFlag("1");
        device.setStatus("在用");
        labDeviceService.updateById(device);
        if (MyEnum.sync) {
            List<LabCronJob> jobs = new ArrayList<>();
            long time = new Date().getTime();
            jobs.add(new LabCronJob("lab_maintain", "1", time, 1));
            jobs.add(new LabCronJob("lab_device", "1", time, 2));
            labCronJobService.saveBatch(jobs);
        }
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "仪器设备保养(维修)表-通过id删除")
    @ApiOperation(value = "仪器设备保养(维修)表-通过id删除", notes = "仪器设备保养(维修)表-通过id删除")
//	@RequiresPermissions("maintain:lab_maintain:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        labMaintainService.removeById(id);

        if (MyEnum.sync) {
            long time = new Date().getTime();
            String condition = "id='" + id + "'";
            LabCronJob job = new LabCronJob("lab_maintain", "2", condition, time, 1);
            labCronJobService.save(job);
        }

        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "仪器设备保养(维修)表-批量删除")
    @ApiOperation(value = "仪器设备保养(维修)表-批量删除", notes = "仪器设备保养(维修)表-批量删除")
//	@RequiresPermissions("maintain:lab_maintain:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.labMaintainService.removeByIds(Arrays.asList(ids.split(",")));

        if (MyEnum.sync) {
            long time = new Date().getTime();
            String[] idList = ids.split(",");
            labCronJobService.saveCronBatch("lab_maintain","2",time,idList);
        }
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "仪器设备保养(维修)表-通过id查询")
    @ApiOperation(value = "仪器设备保养(维修)表-通过id查询", notes = "仪器设备保养(维修)表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<LabMaintain> queryById(@RequestParam(name = "id", required = true) String id) {
        LabMaintain labMaintain = labMaintainService.getById(id);
        if (labMaintain == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(labMaintain);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param labMaintain
     */
//    @RequiresPermissions("maintain:lab_maintain:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabMaintain labMaintain) {
        return super.exportXls(request, labMaintain, LabMaintain.class, "仪器设备保养(维修)表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
//    @RequiresPermissions("maintain:lab_maintain:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabMaintain.class);
    }

}
